package com.nondo.dean.product.service;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.product.common.CategoryUtils;
import com.nondo.dean.product.domain.*;
import com.nondo.dean.product.mapper.ProductExtendsMapper;
import com.nondo.dean.product.mapper.ProductMapper;
import com.nondo.dean.product.mapper.ProductSeriesMapper;
import com.nondo.dean.product.mapper.SeriesMapper;
import com.nondo.dean.product.model.CategoryBean;
import com.nondo.dean.product.model.result.CategoryResult;
import com.nondo.dean.product.model.result.ProductBeanResult;
import com.nondo.dean.product.model.result.ProductResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@Component
public class SeriesService {
    @Autowired
    SeriesMapper seriesMapper;

    @Autowired
    ProductSeriesMapper productSeriesMapper;

    @Autowired
    ProductCategoryService productCategoryService;

    @Autowired
    AliyunOssTool aliyunOssTool;

    @Autowired
    ProductExtendsMapper productExtendsMapper;

    /**
     * 添加
     *
     * @param series
     * @return
     */
    public int add(Series series) {
        return this.seriesMapper.insertSelective(series);
    }

    /**
     * 更新
     *
     * @param series
     * @return
     */
    @Transactional
    public int update(Series series) {
        return this.seriesMapper.updateByPrimaryKeySelective(series);
    }

    /**
     * 删除
     *
     * @param series
     * @return 0=删除失败，1=删除成功，2=无法删除
     */
    public int delete(Series series) {
        series.setDelFlag(Series.DEL_FLAG_DELETE);
        return this.seriesMapper.updateByPrimaryKeySelective(series);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public Series select(Integer id) {
        return this.seriesMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    public Series selectByName(String name) {
        SeriesExample seriesExample = new SeriesExample();
        seriesExample.createCriteria().andNameEqualTo(name).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);

        List<Series> seriesList = this.seriesMapper.selectByExample(seriesExample);

        return CollectionUtils.isNotEmpty(seriesList) ? seriesList.get(0) : null;
    }

    /**
     * 筛选系列下没有产品的 系列 总数统计
     * @return
     */
    public Long countSeries(){
        return productExtendsMapper.countBySeries();
    }

    /**
     * 筛选系列下没有产品的 系列
     * @param offset
     * @param limit
     * @return
     */
    public List<Series> seriesList(int offset, int limit) {
        SeriesExample example = new SeriesExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        List<Series> seriesList =  productExtendsMapper.selectBySeriesWithRowbounds(rowBounds);
        for(Series seriesBean : seriesList){
            seriesBean.setImage(aliyunOssTool.getImgUrl(seriesBean.getImage()));
        }
        return seriesList;
    }


    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param series
     * @return
     */
    public List<Series> getList(int offset, int limit, Series series) {
        SeriesExample example = new SeriesExample();
        example.setOrderByClause("order_num ASC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        SeriesExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, series);
        List<Series> seriesList =  seriesMapper.selectByExampleWithRowbounds(example, rowBounds);
        for(Series seriesBean : seriesList){
            seriesBean.setImage(aliyunOssTool.getImgUrl(seriesBean.getImage()));
        }
        return seriesList;
    }

    /**
     * 数据统计
     *
     * @param series
     * @return
     */
    public Long count(Series series) {
        SeriesExample example = new SeriesExample();
        SeriesExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, series);
        return seriesMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param series
     */
    void setCriteria(SeriesExample.Criteria criteria, Series series) {
        if (series != null) {
            if (series.getId() != null) {
                criteria.andIdEqualTo(series.getId());
            }
            if (StringUtils.isNoneBlank(series.getName())) {
                criteria.andNameEqualTo(series.getName());
            }
        }
        criteria.andDelFlagEqualTo(Series.DEL_FLAG_NORMAL);
    }

    /**
     * 检查产品下面是否还有品牌关联
     *
     * @param series
     * @return
     */
    public int checkProduct(Series series) {
        ProductSeriesExample productSeriesExample = new ProductSeriesExample();
        productSeriesExample.createCriteria().andSeriesIdEqualTo(series.getId());
        List list = this.productSeriesMapper.selectByExample(productSeriesExample);
        return list.size();
    }

    /**
     * 全部一级分类 第一条数据添加属性id ： 0  name ：{全部}
     * @return
     */
    public  List<ProductCategory>  thePrimaryClassification(Integer categoryId){
        List<ProductCategory> productCategoryList= new ArrayList<>();
        //用于显示全部
        ProductCategory productCategoryBean =new ProductCategory();
        List<ProductCategory> categoryList = productExtendsMapper.queryCategory(categoryId);
        List<CategoryResult> results = CategoryUtils.handle(productCategoryService.categoryList(),categoryList,aliyunOssTool);
        for(CategoryResult beans : results){
            ProductCategory category =new ProductCategory();
            BeanUtils.copy(beans,category);
            productCategoryList.add(category);
        }
        productCategoryBean.setId(0);
        productCategoryBean.setName("全部");
        //全部一级分类
        //List<ProductCategory> productCategoryList= productCategoryService.selectProductCategoryById(0);

        productCategoryList.add(0,productCategoryBean);
        return productCategoryList;
    }
    /**
     * 获取系列下面的所有产品 以及分类下的产品
     * @param seriesId 系列id
     * @param categoryId 一级分类id
     * @param offset
     * @param limit
     * @return
     */
    public List<ProductBeanResult>  getSeriesProduct(Integer seriesId,Integer categoryId,int offset, int limit){
        List<ProductBeanResult> newList=new ArrayList<>();
        ProductBeanResult productBeanResult=new ProductBeanResult();
        newList.add(productBeanResult);

        if(seriesId !=null && categoryId ==0){
            //一级分类
            productBeanResult.setProductCategoryList(thePrimaryClassification(seriesId));
            //系列下所对应的产品
            List<ProductResult> productResultList =new ArrayList<>();
            List<Product> products= productCategoryService.queryProduct(null,seriesId,offset,limit);
            for(Product productResult : products){
                ProductResult bean= ProductResult.covertToBean(productResult);
                bean.setMainImage(aliyunOssTool.getImgUrl(bean.getMainImage()));
                productResultList.add(bean);
            }
            productBeanResult.setProductResultList(productResultList);
        }else if(categoryId !=0 && seriesId !=null){
            //全部一级分类
            productBeanResult.setProductCategoryList(thePrimaryClassification(seriesId));
            //二级分类对应的产品
            List<ProductResult> productResultList =new ArrayList<>();
            CategoryResult result=productCategoryService.getCategoryDetail(categoryId);
            List<CategoryBean> beanList=result.getChildrenList();
            for(CategoryBean bean : beanList){
               List<Product> productList=productCategoryService.queryCategoryProduct(bean.getId(),seriesId,offset,limit);
               for(Product pd : productList){
                   ProductResult productBean= ProductResult.covertToBean(pd);
                   productBean.setMainImage(aliyunOssTool.getImgUrl(productBean.getMainImage()));
                   productResultList.add(productBean);
               }
            }
            productBeanResult.setProductResultList(productResultList);
        }

        return newList;
    }
}
